#!/bin/bash

set -xe

kubectl create ns kubeflow || echo "namespace kubeflow already exists"
kustomize build bentoml-yatai-stack/default | kubectl apply --server-side -f -
kubectl -n kubeflow wait --for=condition=available --timeout=600s deploy/yatai-image-builder
kubectl -n kubeflow wait --for=condition=available --timeout=600s deploy/yatai-deployment
sleep 5
kubectl apply -n kubeflow -f deployment_from_bento.yaml
sleep 5
kubectl -n kubeflow logs deploy/yatai-deployment
sleep 5
kubectl -n kubeflow wait --for=condition=available --timeout=600s deploy/fraud-detection
kubectl -n kubeflow wait --for=condition=available --timeout=600s deploy/fraud-detection-runner-0
kubectl -n kubeflow wait --for=condition=available --timeout=600s deploy/fraud-detection-runner-1
kubectl -n kubeflow wait --for=condition=available --timeout=600s deploy/fraud-detection-runner-2

kubectl -n kubeflow port-forward svc/fraud-detection 3333:3000 &
PID=$!

function trap_handler {
 kill $PID
 kubectl -n kubeflow logs -l yatai.ai/bento-deployment=fraud-detection --tail=100
 kubectl -n kubeflow delete -f deployment_from_bento.yaml
 kustomize build bentoml-yatai-stack/default | kubectl delete -f -
}

trap trap_handler EXIT

sleep 5

# FIXME: getting AttributeError: 'ColumnTransformer' object has no attribute '_name_to_fitted_passthrough'
# output=$(curl --fail -X 'POST' \
#   'http://localhost:3333/is_fraud' \
#   -H 'accept: application/json' \
#   -H 'Content-Type: application/json' \
#   -d '[
#   {
#     "TransactionID": 2987000,
#     "TransactionDT": 86400,
#     "TransactionAmt": 68.5,
#     "ProductCD": "W",
#     "card1": 13926,
#     "card2": null,
#     "card3": 150,
#     "card4": "discover",
#     "card5": 142,
#     "card6": "credit",
#     "addr1": 315,
#     "addr2": 87,
#     "dist1": 19,
#     "dist2": null,
#     "P_emaildomain": null,
#     "R_emaildomain": null,
#     "C1": 1,
#     "C2": 1,
#     "C3": 0,
#     "C4": 0,
#     "C5": 0,
#     "C6": 1,
#     "C7": 0,
#     "C8": 0,
#     "C9": 1,
#     "C10": 0,
#     "C11": 2,
#     "C12": 0,
#     "C13": 1,
#     "C14": 1,
#     "D1": 14,
#     "D2": null,
#     "D3": 13,
#     "D4": null,
#     "D5": null,
#     "D6": null,
#     "D7": null,
#     "D8": null,
#     "D9": null,
#     "D10": 13,
#     "D11": 13,
#     "D12": null,
#     "D13": null,
#     "D14": null,
#     "D15": 0,
#     "M1": "T",
#     "M2": "T",
#     "M3": "T",
#     "M4": "M2",
#     "M5": "F",
#     "M6": "T",
#     "M7": null,
#     "M8": null,
#     "M9": null,
#     "V1": 1,
#     "V2": 1,
#     "V3": 1,
#     "V4": 1,
#     "V5": 1,
#     "V6": 1,
#     "V7": 1,
#     "V8": 1,
#     "V9": 1,
#     "V10": 0,
#     "V11": 0,
#     "V12": 1,
#     "V13": 1,
#     "V14": 1,
#     "V15": 0,
#     "V16": 0,
#     "V17": 0,
#     "V18": 0,
#     "V19": 1,
#     "V20": 1,
#     "V21": 0,
#     "V22": 0,
#     "V23": 1,
#     "V24": 1,
#     "V25": 1,
#     "V26": 1,
#     "V27": 0,
#     "V28": 0,
#     "V29": 0,
#     "V30": 0,
#     "V31": 0,
#     "V32": 0,
#     "V33": 0,
#     "V34": 0,
#     "V35": null,
#     "V36": null,
#     "V37": null,
#     "V38": null,
#     "V39": null,
#     "V40": null,
#     "V41": null,
#     "V42": null,
#     "V43": null,
#     "V44": null,
#     "V45": null,
#     "V46": null,
#     "V47": null,
#     "V48": null,
#     "V49": null,
#     "V50": null,
#     "V51": null,
#     "V52": null,
#     "V53": 1,
#     "V54": 1,
#     "V55": 1,
#     "V56": 1,
#     "V57": 0,
#     "V58": 0,
#     "V59": 0,
#     "V60": 0,
#     "V61": 1,
#     "V62": 1,
#     "V63": 0,
#     "V64": 0,
#     "V65": 1,
#     "V66": 1,
#     "V67": 1,
#     "V68": 0,
#     "V69": 0,
#     "V70": 0,
#     "V71": 0,
#     "V72": 0,
#     "V73": 0,
#     "V74": 0,
#     "V75": 1,
#     "V76": 1,
#     "V77": 1,
#     "V78": 1,
#     "V79": 0,
#     "V80": 0,
#     "V81": 0,
#     "V82": 0,
#     "V83": 0,
#     "V84": 0,
#     "V85": 0,
#     "V86": 1,
#     "V87": 1,
#     "V88": 1,
#     "V89": 0,
#     "V90": 0,
#     "V91": 0,
#     "V92": 0,
#     "V93": 0,
#     "V94": 0,
#     "V95": 0,
#     "V96": 1,
#     "V97": 0,
#     "V98": 0,
#     "V99": 0,
#     "V100": 0,
#     "V101": 0,
#     "V102": 1,
#     "V103": 0,
#     "V104": 0,
#     "V105": 0,
#     "V106": 0,
#     "V107": 1,
#     "V108": 1,
#     "V109": 1,
#     "V110": 1,
#     "V111": 1,
#     "V112": 1,
#     "V113": 1,
#     "V114": 1,
#     "V115": 1,
#     "V116": 1,
#     "V117": 1,
#     "V118": 1,
#     "V119": 1,
#     "V120": 1,
#     "V121": 1,
#     "V122": 1,
#     "V123": 1,
#     "V124": 1,
#     "V125": 1,
#     "V126": 0,
#     "V127": 117,
#     "V128": 0,
#     "V129": 0,
#     "V130": 0,
#     "V131": 0,
#     "V132": 0,
#     "V133": 117,
#     "V134": 0,
#     "V135": 0,
#     "V136": 0,
#     "V137": 0,
#     "V138": null,
#     "V139": null,
#     "V140": null,
#     "V141": null,
#     "V142": null,
#     "V143": null,
#     "V144": null,
#     "V145": null,
#     "V146": null,
#     "V147": null,
#     "V148": null,
#     "V149": null,
#     "V150": null,
#     "V151": null,
#     "V152": null,
#     "V153": null,
#     "V154": null,
#     "V155": null,
#     "V156": null,
#     "V157": null,
#     "V158": null,
#     "V159": null,
#     "V160": null,
#     "V161": null,
#     "V162": null,
#     "V163": null,
#     "V164": null,
#     "V165": null,
#     "V166": null,
#     "V167": null,
#     "V168": null,
#     "V169": null,
#     "V170": null,
#     "V171": null,
#     "V172": null,
#     "V173": null,
#     "V174": null,
#     "V175": null,
#     "V176": null,
#     "V177": null,
#     "V178": null,
#     "V179": null,
#     "V180": null,
#     "V181": null,
#     "V182": null,
#     "V183": null,
#     "V184": null,
#     "V185": null,
#     "V186": null,
#     "V187": null,
#     "V188": null,
#     "V189": null,
#     "V190": null,
#     "V191": null,
#     "V192": null,
#     "V193": null,
#     "V194": null,
#     "V195": null,
#     "V196": null,
#     "V197": null,
#     "V198": null,
#     "V199": null,
#     "V200": null,
#     "V201": null,
#     "V202": null,
#     "V203": null,
#     "V204": null,
#     "V205": null,
#     "V206": null,
#     "V207": null,
#     "V208": null,
#     "V209": null,
#     "V210": null,
#     "V211": null,
#     "V212": null,
#     "V213": null,
#     "V214": null,
#     "V215": null,
#     "V216": null,
#     "V217": null,
#     "V218": null,
#     "V219": null,
#     "V220": null,
#     "V221": null,
#     "V222": null,
#     "V223": null,
#     "V224": null,
#     "V225": null,
#     "V226": null,
#     "V227": null,
#     "V228": null,
#     "V229": null,
#     "V230": null,
#     "V231": null,
#     "V232": null,
#     "V233": null,
#     "V234": null,
#     "V235": null,
#     "V236": null,
#     "V237": null,
#     "V238": null,
#     "V239": null,
#     "V240": null,
#     "V241": null,
#     "V242": null,
#     "V243": null,
#     "V244": null,
#     "V245": null,
#     "V246": null,
#     "V247": null,
#     "V248": null,
#     "V249": null,
#     "V250": null,
#     "V251": null,
#     "V252": null,
#     "V253": null,
#     "V254": null,
#     "V255": null,
#     "V256": null,
#     "V257": null,
#     "V258": null,
#     "V259": null,
#     "V260": null,
#     "V261": null,
#     "V262": null,
#     "V263": null,
#     "V264": null,
#     "V265": null,
#     "V266": null,
#     "V267": null,
#     "V268": null,
#     "V269": null,
#     "V270": null,
#     "V271": null,
#     "V272": null,
#     "V273": null,
#     "V274": null,
#     "V275": null,
#     "V276": null,
#     "V277": null,
#     "V278": null,
#     "V279": 0,
#     "V280": 0,
#     "V281": 0,
#     "V282": 1,
#     "V283": 1,
#     "V284": 0,
#     "V285": 0,
#     "V286": 0,
#     "V287": 0,
#     "V288": 0,
#     "V289": 0,
#     "V290": 1,
#     "V291": 1,
#     "V292": 1,
#     "V293": 0,
#     "V294": 1,
#     "V295": 0,
#     "V296": 0,
#     "V297": 0,
#     "V298": 0,
#     "V299": 0,
#     "V300": 0,
#     "V301": 0,
#     "V302": 0,
#     "V303": 0,
#     "V304": 0,
#     "V305": 1,
#     "V306": 0,
#     "V307": 117,
#     "V308": 0,
#     "V309": 0,
#     "V310": 0,
#     "V311": 0,
#     "V312": 0,
#     "V313": 0,
#     "V314": 0,
#     "V315": 0,
#     "V316": 0,
#     "V317": 117,
#     "V318": 0,
#     "V319": 0,
#     "V320": 0,
#     "V321": 0,
#     "V322": null,
#     "V323": null,
#     "V324": null,
#     "V325": null,
#     "V326": null,
#     "V327": null,
#     "V328": null,
#     "V329": null,
#     "V330": null,
#     "V331": null,
#     "V332": null,
#     "V333": null,
#     "V334": null,
#     "V335": null,
#     "V336": null,
#     "V337": null,
#     "V338": null,
#     "V339": null
#   }
# ]')

# echo "output: '${output}'"
# if [[ $output != *'false'* ]]; then
#   echo "Test failed"
#   exit 1
# fi
